/**
 * 将 require('') 转换为 require('')
 * 并且忽略 require('./')
 */
const glob = require('glob');
const file = require('fs');
const path = require('path');

const tasks = [];

const task = (f) => {
    console.log(`dear ${f}`);
    const content = file.readFileSync(f, 'utf-8')
        .replace(/ = require\('.\//g, " = eval('required')('./")
        .replace(/ = require\('..\//g, " = eval('required')('../")
        .replace(/ = require\('/g, " = eval('require')('")
        .replace(/ = eval\('required'\)/g, ' = require');
    file.writeFileSync(f, content);
    if (tasks.length) {
        tasks.pop()();
    } else {
        console.log('task over');
    }
};

glob('./../src/**/*.js', (err, files) => {
    console.log(files);
    files.map((f) => {
        tasks.push(task.bind(null, path.join(__dirname, f)));
    });
    tasks.pop()();
});
